Welcome to “Introduction to Flask.” After watching this video, you will be able
to: Define the Flask web framework and describe
its main features. Explain how to install Flask on your machines. Describe main dependencies of Flask. Explain the main differences between Flask
and another Python web framework called Django. Flask is a micro framework that can create
web applications. It is not opinionated like some of the other
larger frameworks and does not bind the user to a specific set of tools. One of the complex dependencies of Flask is
Python. You will run Flask 2.2.2, which requires a
minimum python version of 3.7. In 2004, Armin Ronacher created this framework
as an April Fool’s joke. It quickly gained popularity for its ease
of use and extensibility. Flask provides minimal dependencies you might
need to create a web application. However, it is extensible, and many community
extensions add additional features to Flask. The main features of Flask include: Flask has a web server that runs applications
in development mode. Flask also comes with a debugger to help debug
applications. The debugger shows interactive traceback and
stack trace in the browser. Flask uses the standard python logging for
application logs. You can use the same logger to log custom
messages about your application. Flask provides a way to test different parts
of your application. The testing feature enables developers to
follow a test-driven approach. You can use frameworks like pytest and coverage
to ensure your code works as desired. The developers can access the request and
response objects to pull arguments and customize responses. Additional features of Flask are the following: The framework also supports static assets
like CSS files, JavaScript files, and images. Flask provides tags to load static files in
the templates. You can also develop dynamic pages using Jinja
templating framework. These dynamic pages can display information
that may change for each request or may check if the user is logged in. Flask provides routing and supports dynamic
URLs that are extremely useful for RESTful services. You can create routes for different HTTP methods
and provide redirection in your application. You can write global error handlers in Flask
that work on the application level. Finally, it supports user session management. Some popular community extensions that can
be added to your application are the following: Flask-SQL Alchemy adds support for an ORM
called SQL Alchemy to Flask, giving developers a way to work with database objects in Python. Flask-Mail provides the ability to set up
an SMTP mail server. Flask-Admin lets you add admin interfaces
to Flask applications easily. Flask-Uploads allows you to add customized
file uploading to your application. Here are some other extensions: Flask-CORS allows your application to handle
Cross-Origin Resource Sharing making cross-origin JavaScript requests possible. Flask-Migrate adds database migrations to
SQL Alchemy ORM. Flask-User adds user authentication, authorization,
and other user management activities. Marshmallow adds extensive object serialization/deserialization
support to your code. Finally, Celery is a powerful task queue that
can be used for simple background tasks and complex multi-stage programs and schedules. Flask is available on the python package manager
called pip. Pip is available in the lab environment. If installing on your machines, it is recommended
that you first create a virtual environment using the venv or pipenv module. You can install Flask version 2.2.2. It is recommended to pin the version number
of the dependencies in your application. This ensures that the application can be reproduced
from scratch in different environments like development, staging, and production. It also ensures new issues and bugs are not
introduced by mistake when packages are updated automatically without a version number. Flask comes with some built-in dependencies
that enable the various features. Werkzeug implements WSGI or the Web Server
Gateway Interface. This is the standard Python interface between
applications and servers. Jinja is a template language that renders
the pages in your application. MarkupSafe comes with Jinja. It escapes untrusted input when rendering
templates to avoid injection attacks. ItsDangerous is used to sign data securely. This helps determine if data has been tampered
with and is used to protect Flask’s session cookie. Click is a framework for writing command-line
applications. It provides the flask command and allows adding
custom management commands. To see the built-in dependencies you can use the pip freeze command to list
currently installed packages in the virtual environment. You can see that all the in-built packages
are installed by default. Now another Python
developer framework, Django. Here are some key differences between Flask
and Django: Flask aims to be a very light framework. Django, on the other hand, is a full-stack
framework. As a result, Flask only provides the basic
dependencies needed to create a web application. It, however, is very extensible and lets the
developer choose extensions that provide additional features. Django, on the other hand, includes everything
you need to create a full-stack application. Flask is very flexible. You can add and remove pieces in a very plug-and-play
manner. On the other hand, Django is very opinionated
and makes most decisions for the developer so they can focus on the application’s logic. In this video, you learned that: Flask is a microframework that ships with
minimal dependencies. To build websites, Flask has features like
debugging servers, routing, templates, and error handling. Flask can be extended by using community extensions. Flask can be installed as a python package. Django is a full-stack framework compared
to Flask.